# -*- encoding: utf-8 -*-
"""
Copyright (c) 2023 - present SKBioinfo
"""

import os, random, string

class Config(object):
    basedir = os.path.abspath(os.path.dirname(__file__))
    # Set up the upload dir 
    document_path = basedir + "/document/"
    os.makedirs(document_path, exist_ok=True)

    # Set up the App SECRET_KEY
    SECRET_KEY  = os.getenv('SECRET_KEY', None)
    if not SECRET_KEY:
        SECRET_KEY = ''.join(random.choice( string.ascii_lowercase  ) for i in range( 32 ))

    # CDN Support Settings 
    CDN_DOMAIN  = os.getenv('CDN_DOMAIN' , None)

    SQLALCHEMY_TRACK_MODIFICATIONS = False
    DB_ENGINE   = os.getenv('DB_ENGINE'   , "mysql+pymysql")
    DB_USERNAME = os.getenv('DB_USERNAME' , "wangyi")
    DB_PASS     = os.getenv('DB_PASS'     , "12345678")
    DB_HOST     = os.getenv('DB_HOST'     , "localhost")
    DB_PORT     = os.getenv('DB_PORT'     , "3306")
    DB_NAME     = os.getenv('DB_NAME'     , "test_db")
    # DB_USERNAME = os.getenv('DB_USERNAME' , "root")
    # DB_PASS     = os.getenv('DB_PASS'     , "123")
    # DB_HOST     = os.getenv('DB_HOST'     , "172.19.0.2")

    # DB_ENGINE   = os.getenv('DB_ENGINE'   , None)
    # DB_USERNAME = os.getenv('DB_USERNAME' , None)
    # DB_PASS     = os.getenv('DB_PASS'     , None)
    # DB_HOST     = os.getenv('DB_HOST'     , None)
    # DB_PORT     = os.getenv('DB_PORT'     , None)
    # DB_NAME     = os.getenv('DB_NAME'     , None)
    # USE_SQLITE  = True 

    # try to set up a Relational DBMS
    if DB_ENGINE and DB_NAME and DB_USERNAME:
        try:
            # Relational DBMS: PSQL, MySql
            SQLALCHEMY_DATABASE_URI = '{}://{}:{}@{}:{}/{}'.format(
                DB_ENGINE,
                DB_USERNAME,
                DB_PASS,
                DB_HOST,
                DB_PORT,
                DB_NAME
            )
            USE_SQLITE  = False
        except Exception as e:
            print('> Error: DBMS Exception: ' + str(e) )
            print('> Fallback to SQLite ')    
    if USE_SQLITE:
        # This will create a file in <app> FOLDER
        SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'db.sqlite3')

class ProductionConfig(Config):
    DEBUG = False
    ### Security ###
    SESSION_COOKIE_HTTPONLY = True
    REMEMBER_COOKIE_HTTPONLY = True
    REMEMBER_COOKIE_DURATION = 3600

class DebugConfig(Config):
    DEBUG = True

class API_SETTING():
    url_prefix="http://127.0.0.1:5000/"
    API_config = {
        'USER_API': "/api/user",
        'PROJECT_API': "/api/project",
        'SAMPLE_API': "/api/sample",
        'BIOANA_API': "/api/bioana",
        'NGSLIB_API': "/api/ngsLib",
        'STOCK_API': "/api/stock",
        'EXTRACTION_API': "/api/extraction",
        'FILEUPLOAD_API': "/api/upload",
        'CHARTS_API': "/api/charts",
        'TIME_PICKER_API': "/api/time_picker",
        'ARRAY_API_DEL': "/api/array_del",
        'TABLE_API':'/api/table',
        'RECORD_API':'/api/record'
    }

# Load all possible configurations
config_dict = {
    'Production': ProductionConfig,
    'Debug'     : DebugConfig,
    'API_setting' : API_SETTING
}
